package 序列;

import java.util.Arrays;
import java.util.HashMap;

public class 最长连续序列 {
    public static void main(String[] args) {
        int[] arr = {4,0,-4,-2,2,5,2,0,-8,-8,-8,-8,-1,7,4,5,5,-4,6,6,-3};
        System.out.println(longestConsecutive(arr));
    }

    public static int longestConsecutive(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        Arrays.sort(nums);
        for (int num : nums) {
            map.put(num, 1);
        }
        int max = 0;
        for (int num : nums) {
            int cur = 1;
            while (map.get(num+1) != null && map.get(num+1) == 1){
                map.put(num+1, map.get(num)+1);
                cur++;
                num++;
            }
            max = Math.max(max, cur);
        }
        return max;
    }
}
